package com.qsxc.business.promoter.controller;

import com.qsxc.business.bean.R;
import com.qsxc.business.promoter.bean.req.BindWechatUserReq;
import com.qsxc.business.promoter.bean.req.CashApplyReq;
import com.qsxc.business.promoter.bean.req.UnBindWechatUserReq;
import com.qsxc.business.promoter.bean.vo.CashRecordVo;
import com.qsxc.business.promoter.bean.vo.DivideRecordVo;
import com.qsxc.business.promoter.bean.vo.PromoterFundsInfoVo;
import com.qsxc.business.promoter.bean.vo.PromoterUserInfoVo;
import com.qsxc.dao.service.ISmPromoterCashRecordService;
import com.qsxc.dao.service.ISmPromoterDivideRecordService;
import com.qsxc.dao.service.ISmPromoterService;
import com.qsxc.system.annotation.SysLog;
import com.qsxc.system.exception.CustomException;
import com.qsxc.system.log.LogUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/promoter")
@Api(value = "推广员相关接口", tags = "推广员相关接口")
public class PromoterController {
    @Resource
    private ISmPromoterService smPromoterService;
    @Resource
    private ISmPromoterDivideRecordService smPromoterDivideRecordService;
    @Resource
    private ISmPromoterCashRecordService smPromoterCashRecordService;

    @SysLog("推广员公众号服务")
    @GetMapping(value = "/sendRedirect")
    @ApiOperation(value = "推广员公众号服务", notes = "推广员公众号服务", position = 1)
    public R<String> sendRedirect(@RequestParam(value = "code") String code, @RequestParam(value = "callBackJson", required = false) String callBackJson, @RequestParam(value = "url", required = false) String url, HttpServletResponse response) throws Exception {
        return smPromoterService.sendRedirect(code, callBackJson, url, response);
    }

    @SysLog("推广员微信用户绑定")
    @PostMapping(value = "bindWechatUser.do", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "推广员微信用户绑定", notes = "推广员微信用户绑定", position = 2)
    public R bindWechatUser(@Validated @RequestBody BindWechatUserReq bindWechatUserReq) {
        try {
            return smPromoterService.bindWechatUser(bindWechatUserReq);
        } catch (CustomException e) {
            return R.fail(e.getCode(), e.getMsg());
        } catch (Exception e) {
            LogUtil.error("推广员微信用户绑定异常", e);
            return R.fail("推广员微信用户绑定异常");
        }
    }

    @SysLog("推广员微信用户解绑")
    @PostMapping(value = "unBindWechatUser.do", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "推广员微信用户解绑", notes = "推广员微信用户解绑", position = 3)
    public R unBindWechatUser(@Validated @RequestBody UnBindWechatUserReq unBindWechatUserReq) {
        try {
            return smPromoterService.unBindWechatUser(unBindWechatUserReq);
        } catch (CustomException e) {
            return R.fail(e.getCode(), e.getMsg());
        } catch (Exception e) {
            LogUtil.error("推广员微信用户解绑异常", e);
            return R.fail("推广员微信用户解绑异常");
        }
    }

    @SysLog("根据openid获取推广员用户信息")
    @GetMapping(value = "getWechatUserInfo.do", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "根据openid获取推广员用户信息", notes = "根据openid获取推广员用户信息", position = 4)
    public R<PromoterUserInfoVo> getWechatUserInfo(@RequestParam(value = "openid") String openid) {
        try {
            return smPromoterService.getWechatUserInfo(openid);
        } catch (CustomException e) {
            return R.fail(e.getCode(), e.getMsg());
        } catch (Exception e) {
            LogUtil.error("根据openid获取推广员微信用户信息异常", e);
            return R.fail("根据openid获取推广员微信用户信息异常");
        }
    }

    @SysLog("获取推广员分账记录")
    @GetMapping(value = "getDivideRecord.do", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取推广员分账记录", notes = "获取推广员分账记录", position = 5)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "openid", value = "推广员openid", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "cashType", value = "提现类型 0、可提现 1、待审核 2、已提现")})
    public R<List<DivideRecordVo>> getDivideRecord(@RequestParam(value = "openid") String openid,
                                                   @RequestParam(value = "cashType", required = false) String cashType) {
        try {
            return smPromoterDivideRecordService.getPromoterDivideRecord(openid, cashType);
        } catch (CustomException e) {
            return R.fail(e.getCode(), e.getMsg());
        } catch (Exception e) {
            LogUtil.error("获取推广员分账记录异常", e);
            return R.fail("获取推广员分账记录异常");
        }
    }

    @SysLog("获取推广员提现记录")
    @GetMapping(value = "getCashRecord.do", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取推广员提现记录", notes = "获取推广员提现记录", position = 6)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "openid", value = "推广员openid", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "busiStatus", value = "业务状态 1、已提交 2、审核通过 3、审核不通过")})
    public R<List<CashRecordVo>> getCashRecord(@RequestParam(value = "openid") String openid,
                                               @RequestParam(value = "busiStatus", required = false) String busiStatus) {
        try {
            return smPromoterCashRecordService.getPromoterCashRecord(openid, busiStatus);
        } catch (CustomException e) {
            return R.fail(e.getCode(), e.getMsg());
        } catch (Exception e) {
            LogUtil.error("获取推广员提现记录异常", e);
            return R.fail("获取推广员提现记录异常");
        }
    }

    @SysLog("获取推广员资金信息")
    @GetMapping(value = "getFundsInfo.do", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取推广员资金信息", notes = "获取推广员资金信息", position = 7)
    public R<PromoterFundsInfoVo> getFundsInfo(@RequestParam(value = "openid") String openid) {
        try {
            return smPromoterService.getFundsInfo(openid);
        } catch (CustomException e) {
            return R.fail(e.getCode(), e.getMsg());
        } catch (Exception e) {
            LogUtil.error("获取推广员资金信息异常", e);
            return R.fail("获取推广员资金信息异常");
        }
    }

    @SysLog("推广员提现申请")
    @PostMapping(value = "cashApply.do", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "推广员提现申请", notes = "推广员提现申请", position = 8)
    public R cashApply(@Validated @RequestBody CashApplyReq cashApplyReq) {
        try {
            return smPromoterCashRecordService.saveCashApply(cashApplyReq);
        } catch (CustomException e) {
            return R.fail(e.getCode(), e.getMsg());
        } catch (Exception e) {
            LogUtil.error("推广员提现申请异常", e);
            return R.fail("推广员提现申请异常");
        }
    }
}
